#!/bin/bash

voxceleb1_path=/work/jason410/PublicData/Voxceleb1
trials_path=data/trials.lst

nnet_type=ResNet34_quarter
pooling_type=ASP
pooling_type=SAP

ckpt_path=ckpt/resnet34_quarter_asp_256.ckpt

. ./path.sh

stage=4
echo stage $stage

# format data dir structure by soft link
if [ $stage -eq 0 ];then
    if [ ! -d data/wav_files ]; then
        mkdir -p data/wav_files
    fi
    rm -rf $trials_path

    # wget https://openslr.magicdatatech.com/resources/49/voxceleb1_test_v2.txt
	wget https://www.openslr.org/resources/49/voxceleb1_test_v2.txt
    mv voxceleb1_test_v2.txt data/voxceleb1_test_v2.txt
    python3 local/format_trials.py \
	    --voxceleb1_root $voxceleb1_path \
	    --src_trials_path data/voxceleb1_test_v2.txt \
	    --dst_trials_path $trials_path
fi

if [ $stage -eq 1 ];then
	#for pooling_type in ASP SAP; do
	for pooling_type in SAP ASP; do
		ckpt_path=ckpt/resnet34_quarter_${pooling_type}_256.ckpt
		#for epsilon in 5 10 15; do
		for epsilon in 1; do
			adv_save_dir=data/adv_data_eps_${epsilon}_${pooling_type}
			CUDA_VISIBLE_DEVICES=7 python3 -W ignore $SPEAKER_TRAINER_ROOT/adversal_attack.py \
				--checkpoint_path $ckpt_path \
				--nnet_type $nnet_type \
				--pooling_type $pooling_type \
				--restarts 1 \
				--num_iters 5 \
				--epsilon $epsilon \
				--evaluate \
				--adv_save_dir $adv_save_dir \
				--score_save_path ${pooling_type}_test.txt \
				--device cuda \
				--trials_path data/test.lst
				#--trials_path data/adv_data_eps_${epsilon}_${pooling_type}/adv_trials.lst
			done
		done
fi

if [ $stage -eq 2 ];then
	if [ ! -d score ]; then
		mkdir score
	fi

	#for voting_eps in 1 5 15 30 45 60 75 90 120; do
			#for pooling_type in ASP; do
			for pooling_type in SAP; do
				for adv_eps in 1 5 10; do
				#for adv_eps in 15; do
					echo trials_path data/adv_data_eps_${adv_eps}_${pooling_type}/adv_trials.lst
					echo score_save_path score/median_filter_${pooling_type}_score_${adv_eps}.txt 
					CUDA_VISIBLE_DEVICES=7 python3 -W ignore $SPEAKER_TRAINER_ROOT/adversal_defense.py \
						--checkpoint_path ckpt/resnet34_quarter_${pooling_type}_256.ckpt \
						--nnet_type $nnet_type \
						--pooling_type $pooling_type \
						--avd_trial_path data/adv_data_eps_${adv_eps}_${pooling_type}/adv_trials.lst \
						--score_save_path score/median_filter_${pooling_type}_score_${adv_eps}.txt \
						--evaluate 
						#--epsilon $voting_eps
				done
			done
	#done
fi


if [ $stage -eq 3 ];then
	for pooling_type in ASP SAP; do
		{
		voting_eps=120
		CUDA_VISIBLE_DEVICES=7 python3 -W ignore $SPEAKER_TRAINER_ROOT/adversal_defense.py \
			--checkpoint_path ckpt/resnet34_quarter_${pooling_type}_256.ckpt \
			--nnet_type $nnet_type \
			--pooling_type $pooling_type \
			--avd_trial_path data/eval.lst \
			--score_save_path score/${pooling_type}_genuine_score_${voting_eps}.txt \
			--epsilon $voting_eps \
			--evaluate
		}&
	done
	wait
fi


if [ $stage -eq 4 ];then
	for pooling_type in ASP SAP; do
		for std in 1 10 15 30; do
			CUDA_VISIBLE_DEVICES=0 python3 -W ignore $SPEAKER_TRAINER_ROOT/voting_attack.py \
				--num_voting 5 \
				--checkpoint_path ckpt/resnet34_quarter_${pooling_type}_256.ckpt \
				--nnet_type $nnet_type \
				--pooling_type $pooling_type \
				--adv_save_dir=data/voting_${std}_adv_data_eps_5_${pooling_type} \
				--epsilon 5 \
				--num_iters 5 \
				--trials_path data/eval.lst \
				--score_save_path ${pooling_type}_${std}.txt \
				--std $std \
				--evaluate 
			done
		done
fi

if [ $stage -eq 5 ];then
	for pooling_type in ASP SAP; do
		for voting_eps in 1 10 15; do
			CUDA_VISIBLE_DEVICES=7 python3 -W ignore $SPEAKER_TRAINER_ROOT/adversal_defense.py \
				--checkpoint_path ckpt/resnet34_quarter_${pooling_type}_256.ckpt \
				--nnet_type $nnet_type \
				--pooling_type $pooling_type \
				--avd_trial_path data/voting_adv_data_eps_5_${pooling_type}/adv_trials.lst \
				--score_save_path score/${pooling_type}_perfect_score_${voting_eps}.txt \
				--epsilon $voting_eps \
				--evaluate
			done
		done
fi

